﻿using DevExpress.XtraEditors;

using DevTeamIT;
using Extensions;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Utility
{
    public class LogManager
    {
        public static void Notify(Exception ex)
        {
            XtraMessageBox.Show(ex.Message, "Kullanıcı Hatası", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        public static int TakeScreenShot()
        {
            MemoryStream memoryStream = new System.IO.MemoryStream();
            Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            byte[] imageBytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                bmpScreenshot.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                imageBytes = ms.ToArray();
            }
            object result = Session.DataManager.ExecuteScalar("InsScreenshot", Session.CurrentUser.UserId, imageBytes);
            return (int)result;
        }

        public static void LogToEventLog(UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            try
            {
                // Since we can't prevent the app from terminating, log this to the event log.
                if (!EventLog.SourceExists("ThreadException"))
                    EventLog.CreateEventSource("ThreadException", "Application");

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "ThreadException";
                myLog.WriteEntry("ConfExpert'te hata oluştu. Hata Bilgisi :\n\n" + ex.ToString() + "\n\nStack Trace:\n" + ex.StackTrace);
            }
            catch
            {
                //    XtraMessageBox.Show("Hata kayıt altına alınamadı! Lütfen IT ekibine haber veriniz. Hata: " + ex.ToString(), "Sistem Hatası");
            }
        }

        public static void LogToDB(string str, string detail = "")
        {
            try
            {
                int exceptionId = Session.DataManager.ExecuteScalar("Catch", str + "\r\n\r\n" + detail, Session.Version, Session.CurrentUser.UserId).ToInt();

                if (str.Contains("System.OutOfMemoryException"))
                    XtraMessageBox.Show(Environment.NewLine + "Bellek hatası. Lütfen programı kapatıp açın.", "DİKKAT!");
                else
                    XtraMessageBox.Show(Environment.NewLine + str, "Sistem hatası - " + exceptionId.ToStr());
                //ExceptionInfo ei = new ExceptionInfo(exceptionId, str, detail);
                //ei.ShowDialog();
            }
            catch
            {
                XtraMessageBox.Show("Bağlantınızda sorun olmadığından eminseniz ekran görüntüsü alıp software@karbel.com adresine e-posta gönderin.\r\n\r\n Hata: " + str + "\r\n\r\n" + detail, "Sistem Hatası");
            }
        }

        public static void LogToDB(Exception ex)
        {
            LogToDB(ex.Message, ex.StackTrace);
        }
    }
}